//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.garlicts.framework.base.response;

import java.io.Serializable;

import com.garlicts.framework.base.code.CommonCode;
import com.garlicts.framework.base.code.ICode;

public class Result<T> implements Serializable {
	
	private static final long serialVersionUID = 1952089908144893914L;
	
	private int code;
    private String msg;
    private T data;

    public Result() {
    }

    public Result(ICode commonCode) {
        this.code = commonCode.getCode();
        this.msg = commonCode.getMsg();
    }

    public Result(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Result<T> setICode(ICode code) {
        this.code = code.getCode();
        this.msg = code.getMsg();
        return this;
    }

    public int getCode() {
        return this.code;
    }

    public Result<T> setCode(int code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return this.msg;
    }

    public Result<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public T getData() {
        return this.data;
    }

    public Result<T> setData(T data) {
        this.data = data;
        return this;
    }

    public boolean success() {
        return this.code == CommonCode.SUCCESS.getCode();
    }

    public boolean isCode(ICode code) {
        return this.code == code.getCode();
    }

    public static <T> Result<T> newSuccessResult(T result) {
        return newResult(result, CommonCode.SUCCESS);
    }

    public static <T> Result<T> newFailResult(ICode icode) {
        return newResult(null, icode);
    }

    public static <T> Result<T> newFailResult(ICode icode, T result) {
        return newResult(result, icode);
    }

    public static <T> Result<T> newFailResult(int code, String msg) {
        Result<T> r = new Result<T>();
        r.code = code;
        r.msg = msg;
        return r;
    }

    private static <T> Result<T> newResult(T result, ICode icode) {
        Result<T> r = new Result<T>();
        r.code = icode.getCode();
        r.msg = icode.getMsg();
        r.data = result;
        return r;
    }

}
